package RabbitUI.filetype;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;

import JSON.JSONArray;
import JSON.JSONException;
import JSON.JSONObject;
import Nuplet.Pair;

public class RabbitUIFileType {
	private static JSONObject json;
	public final static String FILETYPEDIR="filetypedir"+File.separatorChar;
	public static Collection<String> getExt() {
		File f=new File(FILETYPEDIR);
		String[] ext=f.list(new filetype_file_filter());
		ArrayList<String> arrlist=new ArrayList<String>();
		for (int i=0;i<ext.length;i++){
			arrlist.add(ext[i].replace(filetype_file_filter.FILETYPE, ""));
		}
		return arrlist;
		
	}
	public static String findExt(String filename){
		if (filename.contains(".")){
			int beginIndex=filename.lastIndexOf(".");
			int endIndex=filename.length();
			if (endIndex>beginIndex){beginIndex++;}
			return filename.substring(beginIndex, endIndex);
			}
		return null;
	}
	public static CharSequence minusExt(String filename) {
		return filename.substring(0, filename.lastIndexOf(".")+1);
	}//TODO JSONizableHashtable. Coming soon
	public static void openFileType(String fileext) throws JSONException, IOException{
		FileReader fr;
			fr = new FileReader(FILETYPEDIR+fileext+filetype_file_filter.FILETYPE);
			BufferedReader b = new BufferedReader(fr);
			String jsonstr="";
			String tmp="";
			while (tmp!=null){
				jsonstr+=tmp;
				tmp=b.readLine();
			}
			json=new JSONObject(jsonstr);
	}
	public static void closeFileType() {
		json=null;
		System.gc();
	}
	public static String getCompile() throws JSONException {
		return json.getString("compile");		
	}
	public static String getRun() throws JSONException {
		return json.getString("run");	
	}
	public static String getIgnoreChar() throws JSONException {
		return json.optString("ignoreChar");	
	}
	public static String getListBadWords() {
		return json.optString("listbadwords");
	}
	public static Hashtable<String,String> getColormap() throws JSONException {
		JSONObject jsoncolor=json.optJSONObject("colormap");
		if (jsoncolor!=null){
			Hashtable<String,String> colormap=new Hashtable<String,String>();
			JSONArray tmparray;
			String[] keys=JSONObject.getNames(jsoncolor);
			if (keys!=null)
				for (int i=0; i< keys.length ;i++){
					tmparray=jsoncolor.getJSONArray(keys[i]);
					if (tmparray!=null)
						for (int j=0; j< tmparray.length();j++){
							colormap.put(tmparray.getString(j), keys[i]);
						}
				}
			return colormap;
		}
		return null;
	}
	public static Collection<Character> getSeparator() throws JSONException {
		ArrayList<Character> separator=new ArrayList<Character>();
		JSONArray tmparray=json.optJSONArray("separator");	
		if (tmparray!=null)
			for (int j=0; j< tmparray.length();j++){
				separator.add(tmparray.getString(j).charAt(0));
			}
		return separator;
	}
	public static Hashtable<String,Pair<String, Pair<String, Boolean>>> getEnvironment_Delimiters() throws JSONException {
		Hashtable<String,Pair<String, Pair<String, Boolean>>> envdelim=new Hashtable<String,Pair<String, Pair<String, Boolean>>>();
		JSONArray tmparray=json.optJSONArray("environment_delimiter");	
		JSONObject tmp;
		Pair<String,Boolean> tmp_Attribute;
		Pair<String, Pair<String, Boolean>> tmpdelim;
		if (tmparray!=null)
			for (int j=0; j< tmparray.length();j++){
				tmp=tmparray.getJSONObject(j);
				tmp_Attribute=new Pair<String,Boolean>(tmp.getString("color"),tmp.getBoolean("analyse_in"));
				tmpdelim=new Pair<String, Pair<String, Boolean>>(tmp.getString("end"),tmp_Attribute);
				envdelim.put(tmp.getString("start"),tmpdelim);
			}
		return envdelim;
	}
	public static String getCommande(String comexec) {
		return json.optString(comexec);
	}
	public static void openDefaultFileType() throws JSONException, IOException {
		openFileType("notdef");
	}
	
	

}
